#include <pthread.h>
#include <fcntl.h>

#include "interface_ada.hpp"
#include "operatingcenter.hpp"

pthread_t the_thread;
int numBus;
int numLigne;
Position *p; 
float speed;
char * message;

void create_operating_center(int nbBus)
{
	OperatingCenter* oc = OperatingCenter::getInstance();
	oc->define_max_bus(nbBus);
	oc->WelcomeHTML(nbBus);
}
void* fonction_thread (void * nil)
{    	
	OperatingCenter* oc = OperatingCenter::getInstance();
	oc->set_bus_pos (numLigne, numBus, p, speed);
	pthread_exit(NULL);
}
void set_bus_position (int _numLigne, int _numBus, Position *_p, float _speed)
{   
	numBus = _numBus;
	numLigne = _numLigne;	
	p = _p;
	speed = _speed;

	// Creation du thread
	if ( pthread_create(&the_thread, NULL, fonction_thread, NULL) )
    {
        perror("Probleme creation processus");
    }
}

void* thread_EmergencyMessage (void * nil)
{    	
		int retour;
	    OperatingCenter* oc = OperatingCenter::getInstance();
		retour = oc->announceProblem ( numBus,message,p);
		pthread_exit((void *)&retour);
}
void announce_problem(int _numBus, char *_message, Position *_p)
{
	int *retour;
	numBus = _numBus;
	p = _p;
	message=_message;
	// Creation du thread
	if (pthread_create(&the_thread, NULL, thread_EmergencyMessage, NULL))
	{
        	perror("Probleme creation processus");
    }
    
    // On attend la fin de l'execution du thread pour recuperer le resultat
	pthread_join(the_thread, (void **)&retour);
	
}

